# _*_ coding : utf-8 _*_
# @Time : 2023/10/12 10:17
# @Author : momo
# @File : utils
# @Project : bert-textcnn
import random

import pandas as pd
import numpy as np

import tensorflow as tf


def add_total(self, fn1):
    _demo = pd.read_excel(fn1)
    # 统计一个公司的标签总数量  "标签数量"
    _demo[self.total_labels_column] = 0
    for c in self.label_columns:
        _demo[self.total_labels_column] += _demo[c].apply(
            lambda x:
            len(str(x).split(";"))
            if x != "" and x is not np.nan
            else 0
        )
    _demo.to_excel("./demo测试数据源(英文版)+总数.xlsx")


   # res表中， 3 4 5 是标签列
    # 芯片->CHAN01003
    def rematch_df(df, rematch_dict):
        print(rematch_dict)
        for i in range(df.shape[0]):
            # 1级
            if df.iloc[i, 3] is not None and df.iloc[i, 3] != "":
                l1 = str(df.iloc[i, 3]).split(";")
                l1 = [rematch_dict[i[i.rfind("-") + 1:]] for i in l1 if i != "nan"]
                df.iloc[i, 3] = ";".join(l1)
            if df.iloc[i, 4] is not None and df.iloc[i, 4] != "":
                l1 = str(df.iloc[i, 4]).split(";")
                l1 = [rematch_dict[i[i.rfind("-") + 1:]] for i in l1 if i != "nan"]
                df.iloc[i, 4] = ";".join(l1)
            if df.iloc[i, 5] is not None and df.iloc[i, 5] != "":
                l1 = str(df.iloc[i, 5]).split(";")
                l1 = [rematch_dict[i[i.rfind("-") + 1:]] for i in l1 if i != "nan"]
                df.iloc[i, 5] = ";".join(l1)

        return df

if __name__=="__main__":
    print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
    # 功臣代码 把数据源中文版转换为英文版
    # demo_src_path="./data/demo测试数据源.xlsx"
    # df_demo_src=pd.read_excel(demo_src_path)
    # rematch_dict=dh.make_rematch_dict(config.ori_node_path)
    # df_to_save=dh.rematch_df(df_demo_src,rematch_dict)
    # df_to_save.to_excel("./data/demo测试数据源(英文版).xlsx",index=False)